package cn.jingyuan.owl.web.oauth2;

import cn.jingyuan.bee.fastjson.FastJsonUtils;
import cn.jingyuan.bee.utils.bean.BeanUtils;
import cn.jingyuan.bee.utils.map.MapUtils;
import cn.jingyuan.swan.cloud.core.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor;

import java.util.Map;

public class DefaultPrincipalExtractor implements PrincipalExtractor {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Override
    public Object extractPrincipal(Map<String, Object> map) {
      /*
            转换用户信息
            转换权限信息
            转换 client 信息
       */
        Result<Map<String, Object>> result = BeanUtils.mapToBean(map, Result.class, false);
        Map<String, Object> userInfoMap = result.getData();

        if (MapUtils.isEmpty(userInfoMap)) {
            return null;
        }

        DefaultUserDetails userDetails = AuthenticationPrincipalHelper.extractUserDetails(userInfoMap);

        log.debug("principal：{}", FastJsonUtils.json(userDetails));

        return userDetails;
    }

}
